package com.easefun.polyv.liveecommerce.modules.chatroom.widget;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.easefun.polyv.liveecommerce.R;
import com.easefun.polyv.liveecommerce.modules.chatroom.PLVLCChatFunctionListener;
import com.easefun.polyv.liveecommerce.modules.chatroom.PLVLCChatMoreAdapter;
import com.lxj.xpopup.core.BottomPopupView;
import com.plv.livescenes.model.interact.PLVChatFunctionVO;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import static com.plv.foundationsdk.utils.PLVSugarUtil.arrayListOf;

/**
 * date: 2023-07-06
 * author: zhy
 * description: 更多
 */
public class PLVMoreDialogView extends BottomPopupView {
    // <editor-fold defaultstate="collapsed" desc="变量">
    //功能列表
    private RecyclerView chatMoreRv;
    //关闭按钮
    private ImageView plvIvPointRewardClose;
    //功能列表适配器
    private PLVLCChatMoreAdapter adapter;
    //每行显示的功能模块数量
    public static int LAYOUT_SPAN_COUNT = 4;
    //初始化支持的功能模块
    private final ArrayList<PLVChatFunctionVO> functionList = arrayListOf(
            new PLVChatFunctionVO("SHARE", R.drawable.icon_share_white, "分享", true),
            new PLVChatFunctionVO("AUDIO", R.drawable.icon_audio_type, "音频模式", true),
            new PLVChatFunctionVO("XL", R.drawable.icon_xl_type, "线路", true),
            new PLVChatFunctionVO("QXD", R.drawable.icon_qxd_type, "清晰度", true),
            new PLVChatFunctionVO("XCK", R.drawable.icon_xck_type, "小窗播放", true)
    );
    //功能响应监听
    private PLVLCChatFunctionListener functionListener;
    private Context mContext;


    public PLVMoreDialogView(@NonNull @NotNull Context context, PLVLCChatFunctionListener functionListener) {
        super(context);
        this.mContext = context;
        this.functionListener = functionListener;
    }

    // </editor-fold >

    @Override
    protected int getImplLayoutId() {
        return R.layout.plv_point_more_window;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        chatMoreRv = findViewById(R.id.plvlc_chat_more_rv);
        plvIvPointRewardClose = findViewById(R.id.plv_iv_point_reward_close);
        if (functionList.size() >= 4) {
            LAYOUT_SPAN_COUNT = 4;
        } else {
            LAYOUT_SPAN_COUNT = functionList.size();
        }
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, LAYOUT_SPAN_COUNT);
        chatMoreRv.setLayoutManager(layoutManager);
        adapter = new PLVLCChatMoreAdapter(LAYOUT_SPAN_COUNT, mContext);
        adapter.setData(functionList);
        adapter.setListener(new PLVLCChatMoreAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String type) {
                dismiss();
                if (functionListener != null) {
                    String data = String.format("{\"event\" : \"%s\"}", type);
                    functionListener.onFunctionCallback(type, data);
                }
            }
        });
        chatMoreRv.setAdapter(adapter);
        plvIvPointRewardClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
    public PLVChatFunctionVO getFunctionByType(String type) {
        for (PLVChatFunctionVO functionVO : functionList) {
            if (type.equals(functionVO.getType())) {
                return functionVO;
            }
        }
        return null;
    }
    public void updateFunctionStatus(@NonNull PLVChatFunctionVO functionVO) {
        for (int i = 0; i < functionList.size(); i++) {
            if (functionVO.getType() != null && functionVO.getType().equals(functionList.get(i).getType())) {
                functionList.set(i, functionVO);
                break;
            }
        }
        adapter.updateFunctionList(functionList);
    }
}
